//
//  ForgetPwdViewController.m
//  YunGou
//
//  Created by macbook on 16/5/18.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ForgetPwdViewController.h"
#import "NSString+Regexp.h"

@interface ForgetPwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneText;
@property (weak, nonatomic) IBOutlet UITextField *passwordText;
@property (weak, nonatomic) IBOutlet UITextField *codeText;
@property (weak, nonatomic) IBOutlet UIButton *gainCodeBtn;

@property (assign,nonatomic) long content;
@property (nonatomic,copy) NSString *contentText;
@property (nonatomic,strong) NSDate *startDate;
@property (strong,nonatomic) NSTimer *timer;
@end

@implementation ForgetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:tap];
}

- (void)hideKeyBoard{
    [self.view endEditing:YES];
}


- (IBAction)gainCodeClicked:(id)sender {
    BOOL isPhoneNum = [NSString isMobileNumber:self.phoneText.text];
    if (isPhoneNum) {
        self.gainCodeBtn.enabled = NO;
        self.content = (arc4random()%1000000) + 100000;
        self.contentText = [NSString stringWithFormat:@"【车佣】您的注册验证码为:%ld",self.content];
        [NetWorkTool sendVerfiWithPhone:self.phoneText.text content:self.contentText success:^(id object) {
            [MBProgressHUD showSuccess:@"发送成功"];
        } failure:^(NSError *error) {
            [MBProgressHUD showError:@"发送失败"];
        }];
        //定时器
        self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
        _startDate = [NSDate date];
        [[NSRunLoop currentRunLoop]addTimer:self.timer forMode:NSRunLoopCommonModes];
    }
}

//辅助函数
- (void)updateTime:(NSTimer*)sender
{
    double deltaTime = [[NSDate date] timeIntervalSinceDate:_startDate];
    int second = 60 - (int)(deltaTime + 0.5);
    if (second < 0.0) {
        [self.timer setFireDate:[NSDate distantFuture]];
        self.gainCodeBtn.enabled = YES;
        [self.gainCodeBtn setTitle:@"重新获取" forState:UIControlStateNormal];
        [self.gainCodeBtn setTitle:@"重新获取" forState:UIControlStateDisabled];
    }else{
        NSString *title = [NSString stringWithFormat:@"%d秒",second];
        [self.gainCodeBtn setTitle:title forState:UIControlStateNormal];
        [self.gainCodeBtn setTitle:title forState:UIControlStateDisabled];
    }
}

- (IBAction)okClicked:(id)sender {
    NSString *phone = _phoneText.text;
    NSString *password = _passwordText.text;
    NSString *code = _codeText.text;
    if (phone.length != 0) {
        if ([code isEqualToString:[NSString stringWithFormat:@"%ld",_content]]) {
            if (password.length != 0) {
                
            }else{
                [MBProgressHUD showError:@"密码不能为空"];
            }
        }else{
            [MBProgressHUD showError:@"验证码输入不正确"];
        }
        
    }else{
        [MBProgressHUD showError:@"号码不能为空"];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    if ([textField returnKeyType] != UIReturnKeyDone) {
        NSInteger nextTag = textField.tag + 1;
        UITextField *nextTextField = [self.view viewWithTag:nextTag];
        [nextTextField becomeFirstResponder];
    }else{
        [textField resignFirstResponder];
    }
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
